package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.DTO.ClazzDTO;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.mapper.ClazzMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @author ch200
 * @description 针对表【clazz(班级表)】的数据库操作Service实现
 * @createDate 2025-03-11 19:10:42
 */

@Slf4j
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz>
        implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    // 分页查询
    @Override
    public PageResult pageQuery(ClazzQueryParam clazzQueryParam) {

        Integer page = clazzQueryParam.getPage();
        Integer pageSize = clazzQueryParam.getPageSize();
        //1.创建分页对象
        Page<Clazz> p = new Page<>(page, pageSize);
        //2.创建条件对象
        QueryWrapper<Clazz> wrapper = new QueryWrapper<>();
        if (clazzQueryParam.getName() != null) {
            wrapper.like("c.name", clazzQueryParam.getName());
        }
        if (clazzQueryParam.getBegin() != null) {
            wrapper.ge("c.begin_date", clazzQueryParam.getBegin());
        }
        if (clazzQueryParam.getEnd() != null) {
            wrapper.le("c.end_date", clazzQueryParam.getEnd());
        }
        // 3. 添加排序条件，根据updateTime倒序排序
        wrapper.orderByDesc("c.update_time");
        // 3. 调用自定义的分页查询方法

        clazzMapper.page(p, wrapper);
        return new PageResult(p.getTotal(), p.getRecords());
    }

    //该接口用于删除班级信息
    @Override
    public void deleteById(Integer id) throws Exception {

        //1. 查询班级下是否有学员
        Integer count = studentMapper.countByClazzId(id);
        if (count > 0) {
            log.error("该班级下有学生, 不能直接删除~");

            throw new RuntimeException("对不起, 该班级下有学生, 不能直接删除~");

        }
        //2. 如果没有, 再删除班级信息
        clazzMapper.deleteById(id);
    }

    //该接口用于根据主键ID查询班级的信息
    @Override
    public ClazzDTO getInfo(Integer id) {
        LambdaQueryWrapper<Clazz> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Clazz::getId, Clazz::getName, Clazz::getRoom, Clazz::getBeginDate, Clazz::getEndDate, Clazz::getMasterId, Clazz::getSubject, Clazz::getCreateTime, Clazz::getUpdateTime)
                .eq(Clazz::getId, id);
        Clazz clazz = clazzMapper.selectOne(wrapper);
        //封装DTO对象
        ClazzDTO clazzDTO = BeanUtil.copyProperties(clazz, ClazzDTO.class);
        return clazzDTO;
    }


    //该接口用于查询所有班级信息
    @Override
    public List<ClazzDTO> listAll() {

//        LambdaQueryWrapper<Clazz> wrapper = new LambdaQueryWrapper<>();
//        wrapper.select(Clazz::getId, Clazz::getName, Clazz::getRoom, Clazz::getBeginDate, Clazz::getEndDate, Clazz::getMasterId, Clazz::getSubject, Clazz::getCreateTime, Clazz::getUpdateTime)

        List<Clazz> clazzList = clazzMapper.selecClazztList();
        List<ClazzDTO> clazzDTOList = BeanUtil.copyToList(clazzList, ClazzDTO.class);
        return clazzDTOList;


    }


}




